package com.shop.dingdongshoppingapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.dingdongshoppingapi.dao.GoodsDao;
import com.shop.dingdongshoppingapi.dao.SearchInfoDao;
import com.shop.dingdongshoppingapi.entity.Goods;
import com.shop.dingdongshoppingapi.entity.SearchInfo;
import com.shop.dingdongshoppingapi.service.intf.GoodsService;
import com.shop.dingdongshoppingapi.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao dao;
    @Autowired
    private SearchInfoDao searchInfoDao;
    /*销量排序*/
    @Override
    public R search(String name, Integer type,int token) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        if(name!=null && name.length()>0){
            goodsQueryWrapper.like("gname", name);
            List<Goods> goods = dao.selectList(goodsQueryWrapper);
            if(goods!=null){
                searchInfoDao.insert(new SearchInfo(token,name));
                return R.ok(goods);
            }

        }else{
        if (type >0) {
            switch (type) {
                case 1: {
                    goodsQueryWrapper.orderByDesc("gnums");
                    List<Goods> goods = dao.selectList(goodsQueryWrapper);
                    if (goods != null) {
                        System.out.println(goods);
                        return R.ok(goods);
                    }
                    break;
                }
                case 2 :{
                    List<Goods> goods = dao.selectList(goodsQueryWrapper);
                    if (goods != null) {
                        return R.ok(goods);
                    }
                    break;
                }
                default: {
                    goodsQueryWrapper.orderByDesc("gprice");
                    List<Goods> goods = dao.selectList(goodsQueryWrapper);
                    if (goods != null) {
                        return R.ok(goods);
                    }
                    break;
                }
            }
        }
        }
        return R.fail();
    }
}
